/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.struts;

import com.cashmate.hibernate.dao.*;
import com.cashmate.hibernate.pojo.*;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.xml.stream.events.Characters;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Lee
 */
public class GetBorrowerInfo_Appr extends ActionSupport {

    private BranchDAO brd = (BranchDAO) ServletActionContext.getServletContext().getAttribute("branchDAO");
    private LoanapplicationDAO lad = (LoanapplicationDAO) ServletActionContext.getServletContext().getAttribute("loanapplicationDAO");
    private BorrowerDAO bord = (BorrowerDAO) ServletActionContext.getServletContext().getAttribute("borrowerDAO");
    private MatrixcategoriesCriteriaDAO mccd = (MatrixcategoriesCriteriaDAO) ServletActionContext.getServletContext().getAttribute("matrixcategoriesCriteriaDAO");
    private MatrixcategoriesDAO mcd = (MatrixcategoriesDAO) ServletActionContext.getServletContext().getAttribute("matrixcategoriesDAO");
    private MatrixcriteriaDAO mcrd = (MatrixcriteriaDAO) ServletActionContext.getServletContext().getAttribute("matrixcriteriaDAO");
    private CreditinvestigationDAO cd = (CreditinvestigationDAO) ServletActionContext.getServletContext().getAttribute("creditinvestigationDAO");
    private BorrowerinfoDAO bid = (BorrowerinfoDAO) ServletActionContext.getServletContext().getAttribute("borrowerinfoDAO");
    private LoansDAO ld = (LoansDAO) ServletActionContext.getServletContext().getAttribute("loansDAO");
    private LoanverificationDAO lvd = (LoanverificationDAO) ServletActionContext.getServletContext().getAttribute("loanverificationDAO");
    private String borrowerinfo;
    private String message;
    private String cat1;
    private String catvalue;
    private String busnotempty;
    private String vehiclenotempty;
    private String osinotempty;
    private String parpnotempty;
    private String nrnotempty;
    private String infnotempty;
    private String docnotempty;
    private String repnotempty;
    private String soinotempty;
    private String colsnotempty;

    public void setCatvalue(String catvalue) {
        this.catvalue = catvalue;
    }

    public void setBorrowerinfo(String borrowerinfo) {
        this.borrowerinfo = borrowerinfo;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getBusnotempty() {
        return busnotempty;
    }

    public void setBusnotempty(String busnotempty) {
        this.busnotempty = busnotempty;
    }

    public String getCat1() {
        return cat1;
    }

    public String getColsnotempty() {
        return colsnotempty;
    }

    public void setColsnotempty(String colsnotempty) {
        this.colsnotempty = colsnotempty;
    }

    public String getDocnotempty() {
        return docnotempty;
    }

    public void setDocnotempty(String docnotempty) {
        this.docnotempty = docnotempty;
    }

    public String getInfnotempty() {
        return infnotempty;
    }

    public void setInfnotempty(String infnotempty) {
        this.infnotempty = infnotempty;
    }

    public String getNrnotempty() {
        return nrnotempty;
    }

    public void setNrnotempty(String nrnotempty) {
        this.nrnotempty = nrnotempty;
    }

    public String getOsinotempty() {
        return osinotempty;
    }

    public void setOsinotempty(String osinotempty) {
        this.osinotempty = osinotempty;
    }

    public String getParpnotempty() {
        return parpnotempty;
    }

    public void setParpnotempty(String parpnotempty) {
        this.parpnotempty = parpnotempty;
    }

    public String getRepnotempty() {
        return repnotempty;
    }

    public void setRepnotempty(String repnotempty) {
        this.repnotempty = repnotempty;
    }

    public String getSoinotempty() {
        return soinotempty;
    }

    public void setSoinotempty(String soinotempty) {
        this.soinotempty = soinotempty;
    }

    public String getVehiclenotempty() {
        return vehiclenotempty;
    }

    public void setVehiclenotempty(String vehiclenotempty) {
        this.vehiclenotempty = vehiclenotempty;
    }

    @Action(value = "/get-borrower-appraisal", results = {
        @Result(name = "input", location = "${cat1}"),
        @Result(name = "success", location = "${cat1}"),
//        
        @Result(name = "error", location = "${cat1}")
    })
    @Override
    public String execute() {
        String[] borinfo = null;
        if (catvalue.equals("appli")) {
            cat1 = "../../loanapplication.jsp?cat=appli";
        } else if (catvalue.equals("credit")) {
            cat1 = "../../loanapplication.jsp?cat=credit";
        } else if (catvalue.equals("appraise")) {
            cat1 = "../../loanapplication.jsp?cat=appraise";
        } else if (catvalue.equals("loanappli")) {
            cat1 = "../../loanapplication.jsp?cat=loanappli";
        } else {
            cat1 = "../../loanapplication.jsp?cat=matrix";
        }

        try {
            borinfo = borrowerinfo.split(" ");
            //borrower id
            char[] numbers = borinfo[1].toCharArray();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < numbers.length; i++) {
                Character chara = numbers[i];
                if (!Character.isLetter(chara)) {
                    sb.append(chara);
                }
            }
//            System.out.println("Stringbuffer:" + sb);
            int inte = Integer.parseInt(sb.toString());
//            System.out.println("PARSED: " + inte);
            //loanver id
            char[] numbers1 = borinfo[0].toCharArray();
            StringBuffer slv = new StringBuffer();
            for (int i = 0; i < numbers1.length; i++) {
                Character chara = numbers1[i];
                if (!Character.isLetter(chara)) {
                    slv.append(chara);
                }
            }
//            System.out.println("Stringbuffer:" + slv);
            int intelv = Integer.parseInt(slv.toString());
//            System.out.println("PARSED: " + intelv);
            
            int lvid = Integer.parseInt(slv.toString());
            int borid = Integer.parseInt(sb.toString());

            Borrowers b = bord.get(borid);
            Loanverification lv = lvd.getById(lvid);
            Loanapplications l = lad.getLatestloan(borid);
            
//            System.out.println(b +".."+lv+".."+l);
            if (b != null) {
                busnotempty = "yes";
                ServletActionContext.getRequest().setAttribute("borrowerinfo", b);
                ServletActionContext.getRequest().setAttribute("borrowerid", b.getBorrowerid());
                ServletActionContext.getRequest().setAttribute("loaninfo", l);
                ServletActionContext.getRequest().setAttribute("loanver", lv);

                message = "ok";
                return SUCCESS;
            } else {
//                cat1 = "../../loanapplication.jsp?cat=appli";
                message = "The borrower you specified was not found.";
                return SUCCESS;
            }
        } catch (Exception e) {
            e.printStackTrace();
//            cat1 = "../../loanapplication.jsp?cat=appli";
            message = "The borrower you specified was not found. :]";
            return SUCCESS;
        }
    }
}
